#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <assert.h>
#include <algorithm>
#include <iostream>

namespace wwj
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin()const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		size_t size() const
		{
			return _size;
		}

		const char* c_str() const
		{
			return _str;
		}

		void swap(string& s);

		string(const char* str = "");
		~string();
		string(const string& s);
		string& operator=(const string& s);

		string& operator=(string tmp);

		void resize(size_t n, char ch = '\0');
		void reserve(size_t n);
		void push_back(char ch);
		void append(const char* str);

		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		void insert(size_t pos, char ch);
		void insert(size_t pos, const char* str);
		void erase(size_t pos = 0, size_t len = npos);
		size_t find(char ch, size_t pos = 0);
		size_t find(const char* str, size_t pos = 0);

		string substr(size_t pos = 0, size_t len = npos);
		bool operator<(const string& s) const;
		bool operator<=(const string& s) const;
		bool operator>(const string& s) const;
		bool operator>=(const string& s) const;
		bool operator==(const string& s) const;
		bool operator!=(const string& s) const;


	private:
		char* _str;
		size_t _size;
		size_t _capacity;

	public:
		const static size_t npos = -1;
	};

	std::ostream& operator<<(std::ostream& out, const string& s);
	std::istream& operator>>(std::istream& in, string& s);
	std::istream& getline(std::istream is, string& str, char delim = '\n');

	template<class T>
	void swap(T& a, T& b)
	{
		T c(a); a = b, b = c;
	}

	inline void swap(string& a, string& b)
	{
		a.swap(b);
	}

}